<!--
 * @Author: xws 1043495328@qq.com
 * @Date: 2023-07-12 17:18:36
 * @LastEditors: xws 1043495328@qq.com
 * @LastEditTime: 2023-08-03 14:55:50
 * @FilePath: \vue3-admin-template\src\views\login\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到我的甄选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-button
            class="login_btn"
            type="primary"
            size="defult"
            @click="login"
            :loading="loading"
          >
            登录
          </el-button>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
// 引入用户相关的小仓库
import useUserStore from '@/store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
// import { getTime } from '@/utils/time'
// 获取路由
const $router = useRouter()
// 获取路由信息
const $route = useRoute()
const useStore = useUserStore()
// 定义变量控制按钮加载的一个效果
const loading = ref(false)
// 储存账号密码
const loginForm = reactive({ username: '', password: '' })
// 获取el-form组件
const loginForms = ref()
const login = async () => {
  // 保证全部表单项校验通过再发请求
  await loginForms.value.validate()
  loading.value = true
  //通知仓库发登录请求
  //成功.首页->展示数据
  //失败.弹出登录失败信息
  try {
    //保证登录成功
    await useStore.userLogin(loginForm)
    //  编写式导航跳转到展示数据首页
    // 判断登录的时候,路由路径当中是否有query参数，如果有就往query参数跳转，没有跳转到首页
    const redirect: any = $route.query.redirect
    $router.push({ path: redirect || '/' })
    // 登录成功的提示信息
    // ElNotification({
    //   type: 'success',
    //   message: '登录成功',
    //   title: 'Hi,' + nowTime + '好！尊贵的'  + '用户',
    // })
    // 登录成功加载效果消失
    loading.value = false
  } catch (error) {
    // 登录失败加载效果消失
    loading.value = false
    // 登录失败的提示信息
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  }
}
// const nowTime = getTime()
// 自定义校验规则需要的函数
const validatorUsername = (rule: any, value: any, callback: any) => {
  //rule:即为校验规则对象
  //value:即为表单元素文本内容
  //函数:如果符合条件cal1Back放行通过即为
  //如果不符合条件callBack方法,注入错误提示信息
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('账号长度至少五位'))
  }
}
const validatorPassword = (rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码长度至少六位'))
  }
}

//定义表单校验需要配置对象
const rules = {
  username: [
    // {
    //   required: true,
    //   min: 3,
    //   max: 14,
    //   message: '账号至少在3-14位之间',
    //   trigger: 'blur',
    // },
    { trigger: 'change', validator: validatorUsername },
  ],
  // trigger出发表单校验时机change->文本发生变化时，blur->失去焦点时候
  password: [
    // { required: true, message: '密码不能为空', trigger: 'blur' }
    { trigger: 'change', validator: validatorPassword },
  ],
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;
    h1 {
      color: white;
      font-size: 40px;
    }
    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }
    .login_btn {
      width: 100%;
    }
  }
}
</style>
